<?php

/**
 * Copyright              [HD框架] (C)2011-2012 houdunwang ，Inc. 
 * Encoding               UTF-8 
 * Version                $Id: newsControl      2012-8-18 13:18:15
 * @author                向军
 * Link                   http://www.houdunwang.com       
 * E-mail                 houdunwangxj@gmail.com
 */
class newsControl extends commonControl {

    function index() {
        $db = K('newsview');
        $catid = isset($_GET['catid']) ? $_GET['catid'] : false;
        $where_catid = $catid ? "news.catid = '$catid'" : '';
        $where_status = isset($_GET['state']) ? "status='{$_GET['state']}'" : '';
        $count = $db->where($where_catid)->where($where_status)->where("recycled=0")->count();
        $page = new page($count, 2);
        $news = $db->join("category")->field("id,title,click,state,news.catid,cname,addtime")->
                        where($where_catid)->where($where_status)->where("recycled=0")->limit($page->limit())->all();
        $this->assign("news", $news);
        $this->assign("page", $page->show(2));
        $this->display();
    }

//添加文章视图
    function add_show() {
        if (!isset($_GET['catid'])) {
            $catdb = M("category");
            $category = $catdb->field("catid,cname,pid")->all();
            $category = arr::channel($category, 'catid', 'pid', 0, 2);
            $this->assign("category", $category);
        }
//        $this->assign("fields", $fields);
        $this->display();
    }

    function add() {
        $db = K("newsrelation");
        $_POST['news_data']['userip'] = ip_get_client();
        $_POST['addtime'] = time();
        $_POST['updatetime'] = time();
        $_POST['thumb'] = isset($_POST['thumb'][1][0]) ? $_POST['thumb'][1][0] : '';
        if ($db->add()) {
            $this->success("文章添加成功!", __CONTROL__ . '/index/mid/' . $_GET['mid'], 3);
        } else {
            $this->error("文章添加失败");
        }
    }

    function edit_show() {
        $catdb = M("category");
        $category = $catdb->field("catid,cname,pid")->all();
        $db = K("newsView");
        $news = $db->join("category,news_data")->find($_GET['id']);
        $category = arr::channel($category, 'catid', 'pid', 0, 2);
        $this->assign("category", $category);
        $this->assign("field", $news);
        $this->display();
    }

//编辑
    function edit() {
        $db = K("newsrelation");
        $_POST['news_data']['userip'] = ip_get_client();
        $_POST['addtime'] = time();
        $_POST['updatetime'] = time();
        $_POST['thumb'] = isset($_POST['thumb'][1][0]) ? $_POST['thumb'][1][0] : '';
        if ($db->save()) {
            $this->success("文章修改成功! ", __CONTROL__.'/index/mid/'.$_GET['mid'], 3);
        } else {
            $this->error("文章修改失败");
        }
    }

//删除文章
    function del() {
        $id = $_GET['id'];
        $db = M("news");
        if ($db->save(array("id" => $id, "recycled" => 1))) {
            $this->success("文件已放入回收站");
        } else {
            $this->error("删除文章失败");
        }
    }

//彻底删除文章
    function del_news() {
        $id = $_GET['id'];
        $news_db = K("newsrelation");
        if ($news_db->join("news_data")->del($id)) {
            $this->success("删除成功");
        } else {
            $this->del("删除文章失败"
            );
        }
    }

//回收站
    function recycled() {
        $db = K('newsview');
        $count = $db->where("recycled=1")->count();
        $page = new page($count, 2);
        $news = $db->join("category")->field("id,title,click,state,news.catid,cname,addtime")->
                        where("recycled=1")->limit($page->limit())->all();
        $this->assign("news", $news);
        $this->assign("page", $page->show(2));
        $this->display();
    }

//还原
    function restore() {
        $id = $_GET['id'];
        $db = M("news");
        if ($db->save(array("id" => $id, "recycled" => 0))) {
            $this->success("文章已还原");
        } else {
            $this->error("还原文章失败，请重试");
        }
    }

}

?>
